package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.ShopCartMapper;
import com.woniuxy.common.mapper.ShopCommodityImageMapper;
import com.woniuxy.common.mapper.ShopCommodityMapper;
import com.woniuxy.common.mapper.ShopMerchantMapper;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.CartException;
import com.woniuxy.joylifeshop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    ShopCartMapper shopCartMapper;

    @Autowired
    ShopCommodityMapper shopCommodityMapper;

    @Autowired
    ShopMerchantMapper shopMerchantMapper;

    @Autowired
    ShopCommodityImageMapper shopCommodityImageMapper;

    /**
    * @Author SooYoo
    * @Description 通过购物车的id集合查到购物车的对象集合
    * @Param
    * @Return
    **/
    @Override
    public List<ShopCart> readCartItemsByCartIdList(List<String> comIdList) {

        try {
            List<ShopCart> shopCarts = new ArrayList<>();
            for (int i = 0 ; i<comIdList.size() ; i++){

                ShopCart shopCart = shopCartMapper.selectByPrimaryKey(comIdList.get(i));
                shopCarts.add(shopCart);
            }
            return shopCarts;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CartException();
        }
    }

    /**
    * @Author SooYoo
    * @Description 删除一个购物车条目
    * @Param
    * @Return
    **/
    @Override
    public void removeCartItem(String cartId) {

        try {
            shopCartMapper.deleteByPrimaryKey(cartId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CartException();
        }
    }




    /**
    * @Author SooYoo
    * @Description 通过用户id找到用户购物车模块
    * @Param
    * @Return  购物车对象  商品对象 商家对象
    **/
    @Override
    public List<ObjectDto> readCartByUserId(String userId) {
        try {
            //先找到购物车条目对象
            ShopCartExample shopCartExample = new ShopCartExample();
            ShopCartExample.Criteria criteria = shopCartExample.createCriteria();
            criteria.andIdEqualTo(userId);
            List<ShopCart> shopCarts = shopCartMapper.selectByExample(shopCartExample);
            //找到商品条目对象
            List<ShopCommodity> shopCommodities = new ArrayList<>();
            for (ShopCart shopCart : shopCarts){
                ShopCommodity shopCommodity = shopCommodityMapper.selectByPrimaryKey(shopCart.getComId());
                shopCommodities.add(shopCommodity);
            }
            //通过商品条目中的merchantid找到merchant对象
            List<ShopMerchant> merchants = new ArrayList<>();
            for (ShopCommodity shopCommodity : shopCommodities){
                ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(shopCommodity.getMerchantId());
                merchants.add(shopMerchant);
            }
            //找到商品条目图片对象
            List<ShopCommodityImage> shopCommodityImages = new ArrayList<>();
            for (ShopCommodity shopCommodity : shopCommodities){
                ShopCommodityImage shopCommodityImage = shopCommodityImageMapper.selectByPrimaryKey(shopCommodity.getImgId());
                shopCommodityImages.add(shopCommodityImage);
            }
            //充填dto
            List<ObjectDto> objectDtos = new ArrayList<>();
            for (int i = 0;i<shopCarts.size(); i++){
                ObjectDto objectDto = new ObjectDto();
                objectDto.setData(shopCarts.get(i));
                objectDto.setData2(shopCommodities.get(i));
                objectDto.setData3(merchants.get(i));
                objectDto.setData4(shopCommodityImages.get(i));
                objectDtos.add(objectDto);
            }
            System.out.println(objectDtos);
            return objectDtos;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CartException();
        }
    }




    /**
    * @Author SooYoo
    * @Description 初始化一个购物车条目
    * @Param
    * @Return
    **/
    @Override
    public String createCartItem(String shopId, String userId) {

        try {
            //先验证重复
            ShopCartExample shopCartExample = new ShopCartExample();
            ShopCartExample.Criteria criteria1 = shopCartExample.createCriteria();
            criteria1.andComIdEqualTo(shopId);
            criteria1.andIdEqualTo(userId);
            List<ShopCart> shopCarts = shopCartMapper.selectByExample(shopCartExample);

            //System.out.println(shopCarts);

            if (shopCarts.isEmpty()){
            //如果userid不匹配,也没有商品,则加一条
                ShopCart shopCart = new ShopCart();
                shopCart.setCartId(UUID.randomUUID().toString());
                shopCart.setComId(shopId);
                shopCart.setId(userId);
                shopCart.setCount(1L);
                shopCartMapper.insert(shopCart);

                return "初始化添加购物车条目成功";

            }else {
            //如果有这个商品,则这个商品数量加1
                shopCarts.get(0).setCount(shopCarts.get(0).getCount()+1);
                shopCartMapper.updateByPrimaryKey(shopCarts.get(0));

                return "追加购物车条目成功";
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw  new CartException();
        }
    }


}
